import java.io.*;
import java.util.Scanner;

public class SupermarketSystem {
    private static final String SERIAL_FILE = "supermarket.ser";

    public static void main(String[] args) {
        Supermarket supermarket;
        try {
            // 尝试读取序列化文件
            ObjectInputStream ois = new ObjectInputStream(new FileInputStream(SERIAL_FILE));
            supermarket = (Supermarket) ois.readObject();
            ois.close();
        } catch (Exception e) {
            // 文件不存在或读取错误，创建新的超市对象
            supermarket = new Supermarket("Moonshot Supermarket", "123 Moonshot Road");
        }

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("请选择操作：1. 开卡 2. 查询积分 3. 退出");
            String option = scanner.nextLine();
            switch (option) {
                case "1":
                    System.out.println("请输入卡号：");
                    String cardNumber = scanner.nextLine();
                    System.out.println("请输入姓名：");
                    String name = scanner.nextLine();
                    System.out.println("请输入积分：");
                    int points = scanner.nextInt();
                    supermarket.addMember(cardNumber, name, points);
                    break;
                case "2":
                    System.out.println("请输入卡号：");
                    cardNumber = scanner.nextLine();
                    Member member = supermarket.getMember(cardNumber);
                    if (member != null) {
                        System.out.println("会员：" + member.getName() + "，积分：" + member.getPoints());
                    } else {
                        System.out.println("未找到该会员");
                    }
                    break;
                case "3":
                    try {
                        // 保存超市对象到序列化文件
                        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(SERIAL_FILE));
                        oos.writeObject(supermarket);
                        oos.close();
                        System.out.println("退出系统，数据已保存");
                        System.exit(0);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    break;
                default:
                    System.out.println("无效操作");
            }
        }
    }
}